/*
 *  Copyright (C) 2010 Fabien Renaud
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.boc.botv.web.controller;

import com.boc.botv.common.Database;
import com.boc.botv.model.Bet;
import com.boc.botv.model.Category;
import com.boc.botv.service.BetManager;
import com.boc.botv.service.CategoryManager;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author Fabien Renaud
 */
@Controller
@RequestMapping("/")
public class SectionController {

  @Autowired
  private BetManager manager;
  @Autowired
  private CategoryManager categoryManager;

  @RequestMapping(value = "sport/{subcat}", method = RequestMethod.GET)
  public String sportHandler(@PathVariable(value = "subcat") String subcat, Model model) {
    if (subcat == null || subcat.equals("")) {
      return "redirect:/index.htm";
    }
    setPage("Sport", subcat, model);
    return "section/index";
  }

  @RequestMapping(value = "television/{subcat}", method = RequestMethod.GET)
  public String tvHandler(@PathVariable(value = "subcat") String subcat, Model model) {
    if (subcat == null || subcat.equals("")) {
      return "redirect:/index.htm";
    }
    setPage("Télévision", subcat, model);
    return "section/index";
  }

  @RequestMapping(value = "actualites/{subcat}", method = RequestMethod.GET)
  public String newsHandler(@PathVariable(value = "subcat") String subcat, Model model) {
    if (subcat == null || subcat.equals("")) {
      return "redirect:/index.htm";
    }
    setPage("Actualités", subcat, model);
    return "section/index";
  }

  private void setPage(String section, String subsection, Model model) {
    model.addAttribute("menu", section.toLowerCase());
    model.addAttribute("submenu", subsection.toLowerCase());
    model.addAttribute("path", "../");

    Database.startTransaction(false);
    List<Category> categories = new ArrayList<Category>();
    if (subsection.equals("index")) {
      subsection = section;
      Category c = categoryManager.getCategory(subsection);
      categories = categoryManager.getChilds(c.getId());
      categories.add(c);
      model.addAttribute("title", section + " :: Tous les paris");
    } else {
      subsection = Character.toUpperCase(subsection.charAt(0)) + subsection.substring(1);
      categories.add(categoryManager.getCategory(subsection));
      model.addAttribute("title", section + " > " + subsection + " :: Tous les paris");
    }

    List<Bet> allBets = new ArrayList<Bet>();
    List<Bet> closestBets = new ArrayList<Bet>();
    for (Category c : categories) {
      allBets.addAll(manager.getCurrentBetsBySubsection(c.getId()));
      closestBets.addAll(manager.getBetsByCloserEndDate(2, c.getId()));
    }

    model.addAttribute("bet", allBets);
    model.addAttribute("betsoon", closestBets);

    Database.closeTransaction();
  }
}
